package com.tanhua.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.Interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.xml.ws.Holder;

@Service
public class SettingsService {

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private QuestionApi questionApi;


    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private BlackListApi blackListApi;


    /**
     * 读取用户通知设置
     * @return
     */
    public ResponseEntity querySettings() {
        //1. 得到当前登陆的用户
        User user = UserHolder.get();

        //2. 根据userid查找该用户的通知设置
        Settings settings = settingsApi.findByUserId(user.getId());

        //3. 根据userid查找该用户的陌生人问题
        Question question = questionApi.findByUserId(user.getId());

        //4. 创建Vo对象，把数据拷贝到Vo对象中
        SettingsVo vo = new SettingsVo();
        if(settings!=null){
            BeanUtils.copyProperties(settings,vo);
        }

        if(question!=null){
            vo.setStrangerQuestion(question.getTxt());
        }else{
            //如果没有陌生人问题，给一个默认值
            vo.setStrangerQuestion("你真的爱我吗？");
        }

        vo.setPhone(user.getMobile());

        //5. 返回
        return ResponseEntity.ok(vo);
    }


    //保存通知设置
    public ResponseEntity saveNotification(Settings settings) {
        //1. 获取登陆用户
        User user = UserHolder.get();

        //2. 根据用户的id查找通知设置
        Settings dbSettings = settingsApi.findByUserId(user.getId());

        //3. 判断当前用户id是否可以查找到设置，因为有些用户是没有设置
        if(dbSettings==null){
            //4. 如果之前没有设置过通知的用户，保存通知设置
            settings.setUserId(user.getId());
            settingsApi.save(settings);

        }else {
            //5. 如果之前已经有通知设置的用户，更新通知
            settings.setId(dbSettings.getId());
            settingsApi.update(settings);
        }

        //6. 返回响应
        return ResponseEntity.ok(null);


    }

    //更新或者保存陌生人问题
    public ResponseEntity saveQuestion(String content) {
        //1. 获取到当前登录的用户
        User user = UserHolder.get();

        //2. 根据用户id查找用户陌生人问题
        Question question = questionApi.findByUserId(user.getId());
        //3. 判断是否存在陌生人问题，存在就更新，不存在就保存
        if(question==null){
            question = new Question();
            question.setUserId(user.getId());
            question.setTxt(content);
            questionApi.save(question);
        }else{
            //更新
            question.setTxt(content);
            questionApi.update(question);
        }

        //4. 返回响应
        return ResponseEntity.ok(null);
    }


    /**
     * 黑名单列表
     * @param pagesize 页面大小
     * @return
     */
    public ResponseEntity blacklist(Integer pageNum, Integer pagesize) {
        //1. 获取当前登陆的用户
        User user = UserHolder.get();

        //2. 根据用户的id查询当前用户的黑名单，得到Page对象。
       Page<UserInfo> page =  userInfoApi.findBlackList(pageNum,pagesize,user.getId());

        //3. 创建PageResult对象，把page的数据封装给pageResult
        PageResult pageResult = new PageResult(pageNum,pagesize,(int)page.getTotal(),page.getRecords());

        //4. 返回响应对象，里面存储的是pageResult
        return ResponseEntity.ok(pageResult);

    }

    //移除黑名单
    public ResponseEntity deleteBlackUser(Long blackUserId) {
        //1. 获取当前登陆的用户id
        long userId = UserHolder.getUserId();
        //2. 删除黑名单
        blackListApi.deleteBlackUser(userId,blackUserId);

        //3 返回响应即可
        return ResponseEntity.ok(null);
    }
}
